#include "main.h"
jmp_buf errorBuf;
_env envInf = {NULL};
int main (int argc, char * argv[])
{
	int i = 0;
	int m = 0;
	int count = 1;
	int flag = 0;
	int sndFlag = 0;
	char ** fileNames = NULL;
	int erCode = 0;
	if ( (erCode = setjmp (errorBuf)) != 0) {
		system ("pause");
		return 0;
	}
	for (i = 1; i<argc; i++)	{	
		if (argv[i][0] == '-')	{
				switch (argv[i][1]) {          //I use next values of flag: 1 - adding files, 2 - extracting files, 3 - deleting files
				case 'a':	
					if (flag == 0)	{flag = 1;	break;} 
					else {printf ("Many arguments");	system ("pause");	return 0;}
				case 'x':
					if (flag == 0) 	{flag = 2;	break;} 
					else {printf ("Many arguments");	system ("pause");	return 0;}
				case 'd':
					if (flag == 0)	{flag = 3;	break;}
					else {	printf ("Many arguments");	system ("pause");	return 0;}
				case 'l':
					if (sndFlag < 11)	{sndFlag += 1;	break;}
					else {printf ("Many arguments");	system ("pause");	return 0;}
				case 't':
					if (sndFlag <= 1)	{sndFlag += 10;	break;}
					else {printf ("Many arguments");	system ("pause");	return 0;}
				case 'h':
					help();
					break;
				}
		} else {
		fileNames = (char **) realloc (fileNames,	sizeof(char**)*count);
		fileNames[count - 1] = argv[i];
		count++;}
	}
	fileNames = (char **) realloc (fileNames, sizeof(char**)*count);
	fileNames[count - 1] = NULL;
	if ((count<=2) && (flag != 0))	{printf ( "Need more arguments\n" );		system ("pause");	return 0;} 
	// Checking files on individuals
	i = 0;
	while (fileNames[i])	{
		m = 0;
		while (fileNames[m])	{
			if (i != m)	{
				if (!(strcmp (fileNames[i],fileNames[m])))	{printf ("Similar files\n");	system ("pause");	return 0;}
			}
		m++;
		}	
		i++;
	}
	//working with arguments
	switch (flag)
	{
	case 1:
		addFiles (fileNames);
		break;
	case 2:
		extractFiles (fileNames);
		break;
	case 3:
		deleteFile (fileNames);
		break;
	}
	if (sndFlag != 0)	{
		startTestListFunc (fileNames[0],sndFlag);
	}
	i = 0;

	cl_exit (-1,125);
	return 0;
}